package com.dps.recommend.layer.merage;


import com.dps.recommend.ann.LayerAnn;
import com.dps.recommend.beans.DpsRequest;
import com.dps.recommend.beans.ProcessInterface;
import com.dps.recommend.beans.RecItemDps;
import com.dps.recommend.enums.LayerEnum;
import com.dps.recommend.utils.DpsBaseService;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 处理优先cn=tabOperCn,其次cn=tabAlogCn
 */
@Service
@LayerAnn(layer = LayerEnum.MERAGE)
public class DpsCnV1Merage extends DpsBaseService implements ProcessInterface {
    @Override
    public <R extends RecItemDps> List doProcess(DpsRequest dpsRequest, List<R> list) {
        if (CollectionUtils.isEmpty(list)) {
            return list;
        }

        String tabOper2Cn = (String) dpsRequest.getRoad().getProperties().getOrDefault("tabOperCn", "");
        List<String> cnOperArr = Arrays.asList(tabOper2Cn.split("\\,", -1));
        List result = new ArrayList<>(); //运营数据
        List<String> tabOperCnLis = list.stream().filter(rec -> !Objects.isNull(rec) && cnOperArr.contains(rec.getCn())).map(rec -> {
            result.add(rec);
            return rec.getItemId();
        }).collect(Collectors.toList());
        tabOperCnLis = CollectionUtils.isEmpty(tabOperCnLis) ? Lists.newArrayList() : tabOperCnLis;

        String tabAlog2Cn = (String) dpsRequest.getRoad().getProperties().getOrDefault("tabAlogCn", "");
        List<String> cnAlogArr = Arrays.asList(tabAlog2Cn.split("\\,", -1));
        List<String> finalTabOperCnLis = tabOperCnLis;
        list = list.stream().filter(rec -> !Objects.isNull(rec) && !finalTabOperCnLis.contains(rec.getItemId()) && cnAlogArr.contains(rec.getCn())).collect(Collectors.toList()); //算法数据
        list = CollectionUtils.isEmpty(list) ? Lists.newArrayList() : list;
        result.addAll(list);
        return result;
    }
}
